package com.yuelife.service.controller;

import com.github.pagehelper.PageInfo;
import com.yuelife.service.pojo.SProduct;
import com.yuelife.service.pojo.SStore;
import com.yuelife.service.service.SProductService;
import com.yuelife.service.utils.AliyunOSSUtil;
import com.yuelife.service.utils.AliyunossUtils;
import com.yuelife.service.utils.FileUploadResult;
import com.yuelife.service.utils.ServiceProductDTO;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "SProductController")
@RestController
@RequestMapping("/sProduct")
@CrossOrigin
public class SProductController {

    @Autowired
    private SProductService sProductService;

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;//第一个工具包
    @Resource
    private AliyunossUtils aliyunossUtils;//第二个工具包

    /***
     * SProduct分页条件搜索实现
     * @param sProduct
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "SProduct条件分页查询",notes = "分页条件查询SProduct方法详情",tags = {"SProductController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "SProduct对象",value = "传入JSON数据",required = false) SProduct sProduct, @PathVariable  int page, @PathVariable  int size){
        //调用SProductService实现分页条件查询SProduct
        PageInfo<SProduct> pageInfo = sProductService.findPage(sProduct, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * SProduct分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "SProduct分页查询",notes = "分页查询SProduct方法详情",tags = {"SProductController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SProductService实现分页查询SProduct
        PageInfo<SProduct> pageInfo = sProductService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索商品
     * @param sProduct
     * @return
     */
    @ApiOperation(value = "SProduct条件查询",notes = "条件查询SProduct方法详情",tags = {"SProductController"})
    @PostMapping(value = "/search" )
    public Result<List<SProduct>> findList(@ApiParam(name = "SProduct对象",value = "传入JSON数据",required = false) SProduct sProduct){
        //调用SProductService实现条件查询SProduct
        List<SProduct> list = sProductService.findList(sProduct);
        return new Result<List<SProduct>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SProduct根据ID删除",notes = "根据ID删除SProduct方法详情",tags = {"SProductController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SProductService实现根据主键删除
        sProductService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改SProduct数据
     * @param sProduct
     * @param id
     * @return
     */
    @ApiOperation(value = "SProduct根据ID修改",notes = "根据ID修改SProduct方法详情",tags = {"SProductController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/update/{id}")
    public Result update(@RequestBody @ApiParam(name = "SProduct对象",value = "传入JSON数据",required = false) SProduct sProduct, @PathVariable Integer id){
        //设置主键值
        sProduct.setServiceId(id);
        //调用SProductService实现修改SProduct
        sProductService.update(sProduct);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增SProduct数据
     * @param sProduct
     * @return
     */
    @ApiOperation(value = "SProduct添加",notes = "添加SProduct方法详情",tags = {"SProductController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "SProduct对象",value = "传入JSON数据",required = true) SProduct sProduct){
        //调用SProductService实现添加SProduct
        sProductService.add(sProduct);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    //添加商品同时，上传图片，保存到oss云相册（使用了AliyunOSSUtil工具包）
    @PostMapping("/add")
    public Result addProduct(@RequestParam(value = "file", required = false) MultipartFile file, SProduct sProduct) throws IOException {
        //调用oss工具类上传图片并保存到数据库
        String s = aliyunOSSUtil.uploadFile(file.getInputStream());
        sProduct.setImg(s);
        //添加时间
        sProduct.setCreatetime(new Date());
        //调用SStoreService实现添加SStore
        sProductService.add(sProduct);
        return new Result(true, StatusCode.OK, "添加服务商品成功");
    }


    //添加商品同时，上传图片，保存到oss云相册（使用了AliyunOSSUtil工具包）
    @PostMapping("/update")
    public Result updateProduct(@RequestParam(value = "file", required = false) MultipartFile file, SProduct sProduct) throws IOException {
        //调用oss工具类上传图片并保存到数据库
        String s = aliyunOSSUtil.uploadFile(file.getInputStream());
        sProduct.setImg(s);
        //更改时间
        sProduct.setChangetime(new Date());
        //调用SStoreService实现添加SStore
        sProductService.update(sProduct);
        return new Result(true, StatusCode.OK, "添加服务商品成功");
    }




    /***
     * 根据ID查询SProduct数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SProduct根据ID查询",notes = "根据ID查询SProduct方法详情",tags = {"SProductController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<SProduct> findById(@PathVariable Integer id){
        //调用SProductService实现根据主键查询SProduct
        SProduct sProduct = sProductService.findById(id);
        return new Result<SProduct>(true,StatusCode.OK,"查询成功",sProduct);
    }

    /***
     * 查询SProduct全部数据
     * @return
     */
    @ApiOperation(value = "查询所有SProduct",notes = "查询所SProduct有方法详情",tags = {"SProductController"})
    @GetMapping
    public Result<List<SProduct>> findAll(){
        //调用SProductService实现查询所有SProduct
        List<SProduct> list = sProductService.findAll();
        return new Result<List<SProduct>>(true, StatusCode.OK,"查询成功",list) ;
    }


    /**
     * 查询前三的产品
     * @return
     */
    @GetMapping("/find")
    public Result<List<SProduct>> selectSProduct(){
        List<SProduct> sProducts = sProductService.selectSProduct();
        return new Result<List<SProduct>>(true,StatusCode.OK,"查询成功",sProducts);
    }

    /**
     * 根据商家id查询商品列表
     * @param storeId
     * @return
     */
    @PostMapping("/product")
    @ResponseBody
    public Result<List<SProduct>> selectSProductByStoreId(@RequestParam(value = "storeId",required = true) Integer storeId){
        List<SProduct> sProductsList = sProductService.selectSProductByStoreId(storeId);
        return  new Result<List<SProduct>>(true,StatusCode.OK,"查询成功",sProductsList);
    }


    /***
     * 根据serviceId查询SProduct数据
     * @param
     * @return
     */
    //@ApiOperation(value = "SProduct根据ID查询",notes = "根据ID查询SProduct方法详情",tags = {"SProductController"})
    //@ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/find/{serviceId}")
    public Result<SProduct> findByserviceId(@PathVariable("serviceId") Integer serviceId){
        //调用SProductService实现根据主键查询SProduct
        SProduct sProduct =  sProductService.selectSProductByserviceId(serviceId);
        return new Result<SProduct>(true,StatusCode.OK,"查询成功",sProduct);
    }

    /**
     * 下架商品
     * @param serviceId
     * @return
     */
    @PostMapping("/down")
    public Result updateSProductStatusByserviceId(Integer serviceId){
        sProductService.updateSProductStatusByserviceId(serviceId);
        return new Result(true,StatusCode.OK,"下架成功");

    }

    /**
     * 商家多条件查询列表成功
     * @param serviceProductDTO
     * @return
     */
    @GetMapping("/listServiceByCondition")
    public Result listServiceByCondition(ServiceProductDTO serviceProductDTO) {
        System.out.println("条件"+serviceProductDTO);
        List<SProduct> sProducts = sProductService.listServiceByCondition(serviceProductDTO);
        PageInfo pageInfo = new PageInfo(sProducts);
        return new Result(true,StatusCode.OK,"查询列表成功",pageInfo);
    }





}
